<template>
  <div class="workEdit-box">
    <!-- 工作经历模块 -->
    <div class="workEdit-min-box">
      <!-- 工作经历：时间-公司名称-职位 -->
      <div
        class="project-header"
        @mouseenter="onMouseEnter"
        @mouseleave="onMouseLeave"
      >
        <TitleC
          :attr1="workExperience.time"
          :attr2="workExperience.jobCompany"
          :attr3="workExperience.jobPosition"
          @attrUpdate="onWorkUpdate"
        ></TitleC>
        <div class="tool-box" v-if="toolBoxVisiable">
          <el-button
            circle
            icon="el-icon-remove-outline"
            size="mini"
            class="tool-item"
            @click="btnClick('delete')"
            :disabled="delDisAbled"
          ></el-button>
          <el-button
            circle
            icon="el-icon-circle-plus-outline"
            size="mini"
            class="tool-item"
            @click="btnClick('add')"
            :disabled="addDisAbled"
          ></el-button>
        </div>
      </div>
      <div>
        <AutoTextArea
          v-for="jobDesc in work.jobDescList"
          :key="jobDesc.id"
          :data="jobDesc.desc"
          shape="point"
          @textChange="onJobDescChange(jobDesc.id, $event)"
        ></AutoTextArea>
      </div>
    </div>
  </div>
</template>

<script>
import AutoTextArea from '@/components/base/AutoTextArea.vue'
import TitleC from '@/components/base/TitleC.vue'
import { nanoid } from 'nanoid'
export default {
  data() {
    return {
      // 内容是否可编辑
      edit: false,
      toolBoxVisiable: false,
      work: {
        time: '',
        jobCompany: '',
        jobPosition: '',
        jobDescList: [],
      },
      addDisAbled: false,
      delDisAbled: false,
    }
  },
  props: {
    workExperience: {
      type: Object,
      default: function () {
        return {
          id: nanoid(),
          // 工作时间段
          time: '2016-2020',
          // 工作公司
          jobCompany: '杭州数跑科技',
          // 工作职位
          jobPosition: 'Java开发',
          // 工作描述
          jobDescList: [
            {
              id: '1',
              desc: '负责相关产品的需求以及前端程序的实现，提供合理的前端架构。改进和优化开发工具、开发流程、和开发框架',
            },
            {
              id: '2',
              desc: 'Web前端功能设计、开发和实现，与后台工程师协作，完成数据交互、动态展现',
            },
            {
              id: '3',
              desc: '从视觉和易用性角度，为网站设计提供改进建议，为网站/客户端的页面提供持续优化方案',
            },
          ],
        }
      },
    },
  },
  methods: {
    // 鼠标进入显示工具栏
    onMouseEnter() {
      this.toolBoxVisiable = true
    },
    onMouseLeave() {
      this.toolBoxVisiable = false
    },
    onWorkUpdate({ index, newVal }) {
      switch (index) {
        case 1:
          this.work.time = newVal
          this.workExperienceDataChange()
          break
        case 2:
          this.work.jobCompany = newVal
          this.workExperienceDataChange()
          break
        case 3:
          this.work.jobPosition = newVal
          this.workExperienceDataChange()
          break
        case 4:
          break
      }
    },
    btnClick(tag) {
      switch (tag) {
        case 'add':
          if (this.work.jobDescList.length > 5) {
            this.$message.warning('最多可以有5条数据')
          } else {
            const newDesc = {
              id: nanoid(),
              desc: '自定义内容',
            }
            this.work.jobDescList.push(newDesc)
            this.workExperienceDataChange()
          }
          break
        case 'delete':
          if (this.work.jobDescList.length <= 1) {
            this.$message.warning('至少需要1条数据')
          } else {
            this.work.jobDescList.pop()
            this.workExperienceDataChange()
          }
      }
    },
    onJobDescChange(jobDescId, newVal) {
      this.work.jobDescList.some((item) => {
        if (item.id === jobDescId) {
          item.desc = newVal
          return
        }
      })
      this.workExperienceDataChange()
    },
    // 工作经历组件数据改变
    // 告知父组件，更新数据
    workExperienceDataChange() {
      this.$emit('workInfoUpdate', this.work)
    },
  },
  created() {
    this.work = { ...this.workExperience }
  },
  components: {
    AutoTextArea,
    TitleC,
  },
}
</script>

<style lang="less" scoped>
.workEdit-box {
  width: 100%;
  padding: 0 15px;
}

.project-header {
  position: relative;
}
.tool-box {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 80px;
  height: 40px;
  // font-size: 30px;
  line-height: 40px;
  text-align: center;
}
.tool-item {
  border: none;
  padding: 0;
}
.el-button--mini {
  font-size: 20px;
}
.icon-box {
  display: flex;
  align-items: center;
  border: 1px #eae4e4 solid;
  border-radius: 3px;
  cursor: pointer;
}
</style>
